//
//  UIButton+THKit.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//
#import "UIButton+THKit.h"

@implementation UIButton (THKit)

- (void)layoutButtonWithEdgeInsetsStyle:(JLButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space buttonSize:(CGSize)size
{
    CGFloat imageWidth = self.imageView.intrinsicContentSize.width;
    CGFloat imageHeight = self.imageView.intrinsicContentSize.height;
    CGFloat titleWidth = self.titleLabel.intrinsicContentSize.width;
    CGFloat titleHeight = self.titleLabel.intrinsicContentSize.height;
    CGFloat btnWidth = MAX(size.width, imageWidth + titleWidth);
    CGFloat btnHeight = MAX(size.height, imageHeight + titleHeight);
    
    if (style == JLButtonEdgeInsetsStyleLeft || style == JLButtonEdgeInsetsStyleRight) {
        space = MIN(btnWidth - imageWidth - titleWidth, space);
        space = MAX(space, 0);
        if (style == JLButtonEdgeInsetsStyleLeft) {
            CGFloat offset = space * 0.5;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -offset, 0, offset);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, offset, 0, -offset);
        } else {
            CGFloat imgOffset = titleWidth + space * 0.5;
            CGFloat titleOffset = imageWidth + space * 0.5;
            self.imageEdgeInsets = UIEdgeInsetsMake(0, imgOffset, 0, -imgOffset);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -titleOffset, 0, titleOffset);
        }
    }
    else if (style == JLButtonEdgeInsetsStyleTop || style == JLButtonEdgeInsetsStyleBottom) {
        space = MIN(btnHeight - imageHeight - titleHeight, space);
        space = MAX(space, 0);
        CGFloat blankX = (btnWidth - imageWidth - titleWidth) * 0.5;
        CGFloat blankY = (btnHeight - imageHeight - titleHeight - space) * 0.5;
        CGFloat imgOffsetX = btnWidth * 0.5 - (blankX + imageWidth * 0.5);
        CGFloat titleOffsetX = blankX + imageWidth + titleWidth * 0.5 - btnWidth * 0.5;
        CGFloat imgOffsetY = btnHeight * 0.5 - (blankY + imageHeight * 0.5);
        CGFloat titleOffsetY = btnHeight * 0.5 - (blankY + titleHeight * 0.5);
        
        if (style == JLButtonEdgeInsetsStyleTop) {
            self.imageEdgeInsets = UIEdgeInsetsMake(-imgOffsetY, imgOffsetX, imgOffsetY, -imgOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleOffsetY, -titleOffsetX, -titleOffsetY, titleOffsetX);
        } else {
            self.imageEdgeInsets = UIEdgeInsetsMake(imgOffsetY, imgOffsetX, -imgOffsetY, -imgOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-titleOffsetY, -titleOffsetX, titleOffsetY, titleOffsetX);
        }
    }
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
    }];
}


- (void)layoutButtonWithEdgeInsetsStyle:(JLButtonEdgeInsetsStyle)style imageTitleSpace:(CGFloat)space
{
    CGFloat imageWidth = self.imageView.intrinsicContentSize.width;
    CGFloat imageHeight = self.imageView.intrinsicContentSize.height;
    CGFloat titleWidth = self.titleLabel.intrinsicContentSize.width;
    CGFloat titleHeight = self.titleLabel.intrinsicContentSize.height;
    
    if (style == JLButtonEdgeInsetsStyleLeft || style == JLButtonEdgeInsetsStyleRight) {
        
        CGFloat moveInterver = space * 0.5;
        
        if (style == JLButtonEdgeInsetsStyleLeft) {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, moveInterver, 0, -moveInterver);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -moveInterver, 0, moveInterver);
        } else {
            CGFloat imgOffsetX = titleWidth + moveInterver;
            CGFloat titleOffsetX = imageWidth + moveInterver;
            
            self.imageEdgeInsets = UIEdgeInsetsMake(0, imgOffsetX, 0, -imgOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -titleOffsetX, 0, titleOffsetX);
        }
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(imageWidth + titleWidth + space);
        }];
    }
    else if (style == JLButtonEdgeInsetsStyleTop || style == JLButtonEdgeInsetsStyleBottom) {
        
        CGFloat btnWidth = imageWidth + titleWidth;
        CGFloat btnHeight = imageHeight + titleHeight + space;
        
        CGFloat imgOffsetX = btnWidth * 0.5 - imageWidth * 0.5;
        CGFloat titleOffsetX = (imageWidth + titleWidth * 0.5) - btnWidth * 0.5;
        
        CGFloat imgOffsetY = btnHeight * 0.5 - imageHeight * 0.5;
        CGFloat titleOffsetY = btnHeight * 0.5 - titleHeight * 0.5;
        
        if (style == JLButtonEdgeInsetsStyleTop) {
            self.imageEdgeInsets = UIEdgeInsetsMake(-imgOffsetY, imgOffsetX, imgOffsetY, -imgOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(titleOffsetY, -titleOffsetX, -titleOffsetY, titleOffsetX);
        } else {
            self.imageEdgeInsets = UIEdgeInsetsMake(imgOffsetY, imgOffsetX, -imgOffsetY, -imgOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-titleOffsetY, -titleOffsetX, titleOffsetY, titleOffsetX);
        }
        
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(btnHeight);
        }];
    }
}

@end
